package com.zhou.JavaAndroidFirm.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.adapter.NinePicturesAdapter;
import com.zhou.JavaAndroidFirm.app.AppToast;
import com.zhou.JavaAndroidFirm.base.BaseActivity;
import com.zhou.JavaAndroidFirm.bean.CircleItem;
import com.zhou.JavaAndroidFirm.utils.AppCache;
import com.zhou.JavaAndroidFirm.widget.NoScrollGridView;
import com.zhou.JavaAndroidFirm.widget.NormalTitleBar;
import com.zhou.JavaAndroidFirm.widget.imgsel.PhotoPicker;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 * @date 2018/7/29 0029
 * @des 发布动态
 */
public class CirclePublishActivity extends BaseActivity implements NinePicturesAdapter.OnClickAddListener {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.view_gad)
    View viewGad;
    @BindView(R.id.gridview)
    NoScrollGridView gridview;
    @BindView(R.id.tv_save)
    TextView tvSave;
    private NinePicturesAdapter ninePicturesAdapter;
    private ArrayList<String> selectedPhotos = new ArrayList<>();

    public static void startAction(Context context) {
        Intent intent = new Intent(context, CirclePublishActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_publish_zone;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initView(Bundle bundle) {
        ntb.setTitleText(getString(R.string.zone_publish_title));
        ninePicturesAdapter = new NinePicturesAdapter(mContext, selectedPhotos);
        ninePicturesAdapter.setOnClickAddListener(this);
        gridview.setAdapter(ninePicturesAdapter);
    }

    @Override
    protected void initEvent() {

    }


    @Override
    public void onClickAdd(int positin) {
        PhotoPicker.builder()
                .setPhotoCount(9)
                .setShowCamera(true)
                .setPreviewEnabled(false)
                .setSelected(selectedPhotos)
                .start(this);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PhotoPicker.REQUEST_CODE && resultCode == RESULT_OK) {
            List<String> photos = null;
            if (data != null) {
                photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
            }

            ninePicturesAdapter.clearAll();
            if (photos != null) {
                ninePicturesAdapter.addAll(photos);
            }
        }
    }


    @OnClick({R.id.tv_save, R.id.tv_back})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.tv_save:
                addCircle();
                break;
            case R.id.tv_back:
                finish();
                break;
        }

    }

    /**
     * 新增动态
     */
    private void addCircle() {
        if (TextUtils.isEmpty(etContent.getText().toString())) {
            AppToast.showToast(getString(R.string.circle_publish_empty));
        } else {
            CircleItem circleItem = new CircleItem();
            circleItem.setContent(etContent.getText().toString());
            circleItem.setPictures(getPictureString());
            circleItem.setIcon(AppCache.getInstance().getIcon());
            circleItem.setUserId(AppCache.getInstance().getUserId());
            circleItem.setNickName(AppCache.getInstance().getUserName());
            circleItem.setCreateTime(System.currentTimeMillis());

            EventBus.getDefault().post(circleItem);
            finish();
        }
    }

    /**
     * 对图片进行拼接处理
     *
     * @return
     */
    private String getPictureString() {
        if (selectedPhotos != null && selectedPhotos.size() > 0) {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < selectedPhotos.size(); i++) {
                builder.append(selectedPhotos.get(i)).append(";");
            }
            return builder.substring(0, builder.lastIndexOf(";")).toString();
        }
        return "";
    }
}
